layui.config({base: top.basePath+'/static/common/layui/js/'})
    .define(['table','newLayer','laytpl','newAjax','tableKit'], function(exports) {
    "use strict";

    var $=layui.jquery, laytpl=layui.laytpl,tableKit=layui.tableKit;

    /**
     * @constructor newTable 构造函数
     */
    var newTable = function() {
    	this.target=layui.table;
    	this.needQuery=false;
    	this.querys={
            name:undefined
            ,title:'查询区'
            ,elem:undefined
            ,conditions:undefined
            ,editUrl:undefined
            ,editCallBack:undefined
            ,buttons:[] //{id:'applyMaterial',text:'申请配送记录',fun:undefined}
            ,noRowButtons:[] // 不需要选择直接点击的按钮
            ,multiButtons:[] ////{id:'applyMaterial',text:'申请配送记录',fun:undefined}
            ,notUseCommonButton:false
            ,rowHeight:31
        };
        this.config = tableKit.doConfig({
			id:"newTable"//设定容器唯一ID
            ,elem: "#dataTable"//存放begtable的窗口，必填
            ,openColEdit:true //是否打开列编辑模式
        });
    };

    /**
     * @description 查询条件
     * @param {Object} options
     */
    newTable.prototype.query = function(querys) {
        var that = this;
        that.needQuery=true;
        tableKit.doQuery(that,querys);
        return that;
    };
    /**
     * @description 渲染
     * @param {Object} options
     */
    newTable.prototype.render = function(options) {
        var that = this;
        tableKit.doRender(that,options);
        return that;
    };

    newTable.prototype.search = function() {
        tableKit.doSearch(this);
    };
    newTable.prototype.clear = function() {
        tableKit.doClear(this);
    };

    /**
     * @description 获取选择的行
     */
    newTable.prototype.getSelectedRows = function(needSingle) {
		return tableKit.doGetSelectedRows(this,needSingle);
    };
    /** 选中行 之后回调方法，将选中的单行 或 多行回传 */
    newTable.prototype.callWhenSelectOneRow = function(cb) {
		return tableKit.doCallWhenSelectRows(this,true,cb);
    };
    newTable.prototype.callWhenSelectRows = function(cb) {
		return tableKit.doCallWhenSelectRows(this,false,cb);
    };

    /**
     * @description 获取选择的行的id数组
     */
    newTable.prototype.getSelectedRowIds = function() {
        return tableKit.doGetSelectedRowIds(this);
    };
    newTable.prototype.callWhenSelectRowIds = function(cb) {
        return tableKit.doCallWhenSelectRowIds(this,cb);
    };

    newTable.prototype.bindSearch= function () {
        var that=this;
        tableKit.doBindSearch(this);
        return that;
    }

    newTable.prototype.bindModifyFunction= function () {
        var that=this;
        tableKit.doBindModifyFunction(that);
        return that;
    }
    newTable.prototype.openMixChoose= function () {
        var that=this;
        tableKit.doOpenMixChooseType(that);
        return that;
    }

    newTable.prototype.openNewLayer=function (title,url) {
        var that=this;
       tableKit.doOpenNewLayer(title,url);
        return that;
    }

        /**
         *  打开页面，并获取 被打开页面中选中的行数据，点击确认按钮后回调方法。
         * @param title 被打开页面的标题
         * @param url 被打开页面的url
         * @param needSingle 是否只能选中单行
         * @param callBack 点击确定按钮后的回调函数
         * @param isTreeTable 被打开页面是否是 treeTable结构
         * @returns {newTable}
         */
    newTable.prototype.openChooseLayerSelectRows=function (title,url,needSingle,callBack,isTreeTable) {
        var that=this;
        layui.newLayer.open({
            title: title,
            content: url,
            btn:['确定'],
            yes:function (index,openLayer) {
                var rows=openLayer.find("iframe")[0].contentWindow.layui[isTreeTable?'newTreeTable':'newTable'].getSelectedRows(needSingle);
                if(!rows)return

                callBack(rows);
                layui.newLayer.close(index);
            }
        });

        return that;
    }
        /**
         *  打开页面，并获取 被打开页面中选中的行数据的Id数组，点击确认按钮后回调方法。
         * @param title 被打开页面的标题
         * @param url 被打开页面的url
         * @param callBack 点击确定按钮后的回调函数
         * @param isTreeTable 被打开页面是否是 treeTable结构
         * @returns {newTable}
         */
    newTable.prototype.openChooseLayerSelectIds=function (title,url,callBack,isTreeTable) {
        var that=this;
        layui.newLayer.open({
            title: title,
            content: url,
            btn:['确定'],
            yes:function (index,openLayer) {
                var ids=openLayer.find("iframe")[0].contentWindow.layui[isTreeTable?'newTreeTable':'newTable'].getSelectedRowIds();
                if(!ids || ids.length<1)return;

                callBack(index,openLayer,ids);
            }
        });

        return that;
    }

    /**
     * 弹窗 编辑，确定后回调
     * @param title 抬头说明
     * @param fieldArray 弹窗 中 需要 输入的属性
     * @param cb 提交后 需要 回调函数，会将form的所有属性都返回
     * @returns {newTable}
     */
    newTable.prototype.openEditLayerWithFieldsThenCall=function (title,fieldArray, cb) {
        var that=this;
        tableKit.doOpenEditLayerWithFieldsThenCall(that,title, fieldArray, cb);
        return that;
    }

    newTable.prototype.refreshTable=function (param) {
        var that=this;
        that.target.reload(that.config.id,{
            where: param || ''
            ,page: {curr: 1}
        });
        return that;
    }

    /**
     * 点击 id 打开窗口
     * @param id
     * @param title
     * @param url
     * @param width
     * @param height
     * @returns {newTable}
     */
    newTable.prototype.clickOpen=function (id,title,url,width,height) {
        var that=this;
        $('#'+id).on('click',function () {
            layui.newLayer.open({
                title: title,
                content: url,
            });
        });
        return that;
    }


    var table=new newTable();

    exports('newTable', table);
});